<?php

class RVT{
	// Coger esto del includes/config.php
	var $path = "/Applications/MAMP/htdocs/revealertoolkit-read-only/RVT/";
	// Falta coger esto de la configuracion del RVT
	var $morgue = "/tmp/media/morgue/";
	var $sleuthkit = "/opt/local/bin";
	var $info = "";
	
	function RVT(){
		chdir($this->path);
		putenv('PATH='.$this->sleuthkit.":".getenv("PATH"));
		$this->setInfo();
	}
	
	function setInfo($item){
		$output = array();
		exec('echo "images scan all" | /opt/local/bin/perl RVT.pl -b 2>&1', $output);
		
		$xml = new XMLReader();
		$xml->open($this->morgue.'RVTmorgueInfo.xml');
		$this->info = xml2assoc($xml);
		$xml->close();
	}
	
	function getType($item){
		if ($item == '') return 0;
		$a = split("-",$item);
		return count($a);
	}
	
	function getCase($case){
		foreach ($this->info[0]['value'] as $c)
			if ($c['attributes']['name'] == $case)
				return $c['attributes'];

		return '';
	}
		
	function getDevice($dev){
		$item = split("-",$dev);
		foreach ($this->info[0]['value'] as $case)
			if ($case['attributes']['name']==$item[0])
				foreach ($case['value'] as $device)
					if ($device['attributes']['name']==$item[1]){
						$device['attributes']['name']=$dev;
						return $device['attributes'];
					}
		
		return '';
	}
	
	function getDisk($disk){
		$item = split("-",$disk);
		foreach ($this->info[0]['value'] as $case)
			if ($case['attributes']['name'] == $item[0])
				foreach ($case['value'] as $dev)
					if ($dev['attributes']['name'] == $item[1])
						foreach ($dev['value'] as $dsk)
							if ($dsk['attributes']['name'] == $item[2]){
								$dsk['attributes']['name']=$disk;
								$dsk['attributes']['image']=$case['attributes']['imagepath'].'/'.$item[0].'-'.$case['attributes']['code'].'/'.$disk.'.dd';
								return $dsk['attributes'];
							}
		
		return '';
	}
	
	function getPartition($part){
		$item = split("-",$part);
		$item[3] = str_replace("p","",$item[3]);
		foreach ($this->info[0]['value'] as $case)
			if ($case['attributes']['name'] == $item[0])
				foreach ($case['value'] as $dev)
					if ($dev['attributes']['name'] == $item[1])
						foreach ($dev['value'] as $dsk)
							if ($dsk['attributes']['name'] == $item[2])
								foreach ($dsk['value'] as $par)
									if ($par['attributes']['name'] == $item[3]){
										$par['attributes']['name']=$part;
										return $par['attributes'];
									}

		return '';
	}
	
	function getCases(){
		$c = array();
		foreach ($this->info[0]['value'] as $case)
			$c[] = $case['attributes'];

		return $c;
	}
		
	function getDevices($case){
		$d = array();
		foreach ($this->info[0]['value'] as $c)
			if ($c['attributes']['name']==$case)
				foreach ($c['value'] as $dev){
					$dev['attributes']['name']=$case."-".$dev['attributes']['name'];
					$d[] = $dev['attributes'];
				}
		
		return $d;
	}
	
	function getDisks($device){
		$d = array();
		$item = split("-",$device);
		foreach ($this->info[0]['value'] as $case)
			if ($case['attributes']['name'] == $item[0])
				foreach ($case['value'] as $dev)
					if ($dev['attributes']['name'] == $item[1])
						foreach ($dev['value'] as $disk){
							$disk['attributes']['name']=$device."-".$disk['attributes']['name'];
							$disk['attributes']['image']=$case['attributes']['imagepath'].'/'.$item[0].'-'.$case['attributes']['code'].'/'.$disk['attributes']['name'].'.dd';
							$d[] = $disk['attributes'];
						}
		
		return $d;
	}
	
	function getPartitions($disk){
		$p = array();
		$item = split("-",$disk);
		foreach ($this->info[0]['value'] as $case)
			if ($case['attributes']['name'] == $item[0])
				foreach ($case['value'] as $dev)
					if ($dev['attributes']['name'] == $item[1])
						foreach ($dev['value'] as $dsk)
							if ($dsk['attributes']['name'] == $item[2])
								foreach ($dsk['value'] as $part){
									$part['attributes']['name']=$disk."-p".$part['attributes']['name'];
									$p[] = $part['attributes'];
								}

		return $p;
	}
	
	function exploitDisks($item){
		$res=array();
		$i = split("-",$item);
		
		switch(count($i)){
			case "1": // Case
				$devices = $this->getDevices($item);
				foreach ($devices as $dev){
					$disks = $this->getDisks($dev['name']);
					foreach ($disks as $disk)
						$res[] = $disk['name'];
				}
				break;
			case "2": // Device
				$disks = $this->getDisks($item);
				foreach ($disks as $disk)
					$res[] = $disk['name'];
				break;
			case "3": // Disk
			case "4": // Partition
				$res[] = $i[0]."-".$i[1].'-'.$i[2];
				break;
		}
		
		return $res;
	}
	
	function getFileList($partition){
		global $conf;
		
		$p = split("-",$partition);
		$case = $this->getCase($p[0]);
		$path = $conf['morgue_path'].'/'.$case['name'].'-'.$case['code'].'/'.$p[0].'-'.$p[1].'-'.$p[2].'/output/FileList/'.$partition.'_FileList.csv';

		$res=array();
		if (file_exists($path)){
			$handle = fopen($path, "r");
			if ($handle) {
				while (!feof($handle)) {
					$line = split(",",fgets($handle));
					if ($line[1]=='Allocated' || $line[1]=='Deleted'){
						$line[6] = rtrim($line[6]);
						$dirs = split("/",$line[5]);
						$tmp = &$res[$line[1]];
						for($i=1; $i < (count($dirs)-1); $i++) $tmp = &$tmp['d'][$dirs[$i]]['content'];
						if ($line[3] == "Dir") $tmp['d'][$line[6]]=array('inode'=>$line[0],'content'=>array());
						if ($line[3] == "File") $tmp['f'][$line[6]]=$line[0];
					}
				}
				fclose($handle);
			}
		}

		return $res;
	}
	
	function arrayExplode($itemList){
		$res=array();
    	foreach ($itemList as $item){
			$i = split("-",$item);
			
			switch(count($i)){
				case "1": // Case
					$devices = $this->getDevices($item);
					foreach ($devices as $dev){
						$disks = $this->getDisks($dev['name']);
						foreach ($disks as $disk){
							$parts = $this->getPartitions($disk['name']);
							foreach ($parts as $pt)
								$res[$item][$dev['name']][$disk['name']][$pt['name']] = 1;
						}
					}
					break;
				case "2": // Device
					$disks = $this->getDisks($item);
					foreach ($disks as $disk){
						$parts = $this->getPartitions($disk['name']);
						foreach ($parts as $pt)
							$res[$i[0]][$item][$disk['name']][$pt['name']] = 1;
					}
					break;
				case "3": // Disk
					$parts = $this->getPartitions($item);
					foreach ($parts as $pt)
						$res[$i[0]][$i[0].'-'.$i[1]][$item][$pt['name']] = 1;
						
					break;
				case "4": // Partition
					$res[$i[0]][$i[0].'-'.$i[1]][$i[0].'-'.$i[1].'-'.$i[2]][$item] = 1;
					break;
			}
    	}		
		return $res;
	}
	
/*
	function getCases(){
		$output = array();
		exec("echo 'case list' | /opt/local/bin/perl RVT.pl -b 2>&1", $output);
		
		$res = array();
		foreach ($output as $line)
			if ( preg_match('/^\t([^ ]*) \'(.+)\':/',$line,$match) ) $res[]=$match[1].'-'.$match[2];

		return $res;
	}
	
	function getDevices($case){
		$output = array();
		exec("echo 'case list' | /opt/local/bin/perl RVT.pl -b 2>&1", $output);
		
		$sp = split("-",$case);
		$res = array();
		$f = 0;
		foreach ($output as $line){
			if ( $f ){
				if ( preg_match('/^\t\t([^-]+-[^-]+).+$/',$line,$match) ) $res[]=$match[1];
				else break;
			}
			if ( preg_match('/'.$sp[0].'/',$line) ) $f = 1;
		}			

		return $res;
	}
	
	function getDisks($device){
		$output = array();
		exec("echo 'case list' | /opt/local/bin/perl RVT.pl -b 2>&1", $output);
		
		$res = array();
		foreach ($output as $line)
			if ( preg_match('/\t\t('.$device.'.+)/',$line, $match) ) $res[]=$match[1];

		return $res;
	}
	
	function getPartitions($disk){
		return 1;
		$output = array();
		exec("echo 'case list' | /opt/local/bin/perl RVT.pl -b 2>&1", $output);
		
		$sp = split("-",$case);
		$res = array();
		$f = 0;
		foreach ($output as $line){
			if ( $f )
				if ( preg_match('/^\t\t([^-]+–[^-]+)-.+$/',$line,$match) ) $res[]=$match[1];
				else break;
			if ( preg_match('/'.$sp[0].'/',$line) ) $f = 1;
		}			

		return $res;
	}
*/
}


?>
